//
//  SLChatVideoCell.m
//  MyChatDemo
//
//  Created by DragonYao on 2017/9/26.
//  Copyright © 2017年 DragonYao. All rights reserved.
//

#import "SLChatVideoCell.h"
#import <AVFoundation/AVFoundation.h>

@interface SLChatVideoCell ()

@property (nonatomic, strong) UIImageView *videoShutImage;

@end

@implementation SLChatVideoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //视频截图
        self.videoShutImage = [UIImageView new];
        self.videoShutImage.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playVideo:)];
        [self.videoShutImage addGestureRecognizer:tapGesture];
        [self.contentView addSubview:self.videoShutImage];
    }
    return self;
}

- (void)configCellWithVideoUrl:(NSString *)videoUrl {
    //获取视频的第一针图片
    
}

+ (CGFloat)cellHeightWithVideoUrl:(NSString *)videoUrl {
    
    AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL URLWithString:videoUrl]];
    NSArray *array = asset.tracks;
    CGSize videoSize = CGSizeZero;
    for (AVAssetTrack *track in array) {
        if ([track.mediaType isEqualToString:AVMediaTypeVideo]) {
            videoSize = track.naturalSize;
        }
    }
    if (videoSize.width == 0 || videoSize.height == 0) {
        //使用默认的
        return kMaxImgHeight + kPadding * 4;
    }
    
    CGFloat ratio = videoSize.width/videoSize.height;
    if (ratio > 1) {//说明是长图
        return ratio * kMaxImgWidth + kPadding * 4;
    }
    else {//宽图(如果宽图特别的宽，就以最大宽度为基准)
        CGFloat imgW = kMaxImgHeight/ratio > kMaxImgWidth ? kMaxImgWidth : kMaxImgHeight/ratio;
        return ratio * imgW + kPadding * 4;
    }
}



- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}

@end
